package com.zhongwang.business.module.system.permission;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.common.ValidResult;
import com.zhongwang.business.gen.entity.Permission;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *  权限 控制层
 *
 * @author China.sgl
 * @date 2019/11/28 13:48
 */
@Controller
@RequestMapping("/sys/permission")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 系统权限页面
     * @return 页面 URL
     */
    @GetMapping
    @RequiresPermissions({"system:menu:view"})
    public String page() {
        return "system/permission/page";
    }

    /**
     * 系统权限数据
     * @return 系统帐户 Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    public List<Permission> list(Permission permission) {
        return permissionService.findPermissionAll(permission);
    }

    /**
     * 权限码唯一验证
     *
     * @param permission 权限
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    public ValidResult checkUnique(Permission permission) {
        Permission p = permissionService.findByPermissionName(permission.getPermissionName());
        if (permission.getId() == null) {
            return ValidResult.result(p);
        }
        if (p == null || permission.getId().equals(p.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建权限
     *
     * @param permission 权限信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    public JsonResult save(Permission permission) {
        return jsonResult(permissionService.savePermission(permission));
    }

    /**
     * 根据ID查询
     *
     * @param id id
     * @return Permission
     */
    @ResponseBody
    @PostMapping("/id")
    public Permission id(Integer id) {
        return permissionService.getPermission(id);
    }

    /**
     * 修改权限
     *
     * @param permission 权限信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    public JsonResult update(Permission permission) {
        return jsonResult(permissionService.updatePermission(permission));
    }

    /**
     * 删除权限
     *
     * @param permission 权限
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    public JsonResult del(Permission permission) {
        return jsonResult(permissionService.delPermission(permission));
    }

}
